/**
 * May 21, 2014 10:13:08 AM
 */
package elitech.vietnam.copyweatherfashion.fragments.settings;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.gson.Gson;

import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.dialog.ProgressDialogFragment;
import elitech.vietnam.copyweatherfashion.prefs.PrefsDefinition;

/**
 * @author Cong
 *
 */
public class LanguageFragment extends Fragment implements RadioGroup.OnCheckedChangeListener {

	MainActivity mActivity;
	
	RadioGroup mRgMain;
	RadioButton[] mRadio = new RadioButton[4];
	
	public LanguageFragment() {
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.settings_language, container, false);
		
		mRgMain = (RadioGroup) view.findViewById(R.id.mainLayout);
		mRadio[0] = (RadioButton) view.findViewById(R.id.stlang_auto);
		mRadio[1] = (RadioButton) view.findViewById(R.id.stlang_korea);
		mRadio[2] = (RadioButton) view.findViewById(R.id.stlang_eng);
		mRadio[3] = (RadioButton) view.findViewById(R.id.stlang_vi);

		mRadio[mActivity.mOptions.mLanguage].setChecked(true);
		mRgMain.setOnCheckedChangeListener(this);
		
		return view;
	}
	
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.stlang_auto:
			mActivity.mOptions.mLanguage = 0;
			break;
		case R.id.stlang_korea:
			mActivity.mOptions.mLanguage = 1;
			break;
		case R.id.stlang_eng:
			mActivity.mOptions.mLanguage = 2;
			break;
		case R.id.stlang_vi:
			mActivity.mOptions.mLanguage = 3;
			break;
		default:
			break;
		}
		
		new AsyncTask<Integer, Integer, Boolean>() {
			ProgressDialogFragment mDialog = new ProgressDialogFragment.Builder()
			.setTitle(getString(R.string.loading)).setMessage(getString(R.string.please_wait))
			.build();
			@Override
			protected Boolean doInBackground(Integer... params) {
				mActivity.mPrefs.edit().putString(PrefsDefinition.OPTION, new Gson().toJson(mActivity.mOptions)).commit();
				/*
				 * Put user to restore if user is logged in and not auto login mode
				 */
				if (!mActivity.mOptions.mAutoLogin && mActivity.mUser != null)
					mActivity.mPrefs.edit().putString(PrefsDefinition.USER_LANGUAGE, new Gson().toJson(mActivity.mUser)).commit();
				return null;
			}
			@Override
			protected void onPreExecute() {
				mDialog.show(getFragmentManager());
			}
			@Override
			protected void onPostExecute(Boolean result) {
				mDialog.dismiss();
				Intent intent = new Intent(mActivity, MainActivity.class);
				intent.putExtra(MainActivity.EXTRAS_TABRESUME, 4);
				mActivity.startActivity(intent);
				mActivity.finish();
			}
		}.execute();
	}
}
